using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PerfectPaperPasswords;

/*
 * The default page will passcodes based on a SequenceKey as provided in SequenceKeyTextBox 
 * or generate a new SequenceKey and display it in SequenceKeyTextBox if none is provided.
 * 
 * The number and dimensions of the passcards can be configured through the AppSettings -section in
 * the web.config -file.
 * 
 * After succefully generating passcards the PrintableHyperLink will become visible and point to
 * a PrintVersionHandler ASP.NET handler that will generate a printable XPS version of the 
 * passcards
 */
public partial class _Default : System.Web.UI.Page 
{
    private const string PrintVersionHandler = "Printable.ashx";
    private const string CheckPasscodePage = "CheckPasscode.aspx";

    private int maxColumn = Convert.ToInt32(ConfigurationManager.AppSettings["maxColumn"]);
    private int maxRow = Convert.ToInt32(ConfigurationManager.AppSettings["maxRow"]);
    private int maxPassList = Convert.ToInt32(ConfigurationManager.AppSettings["maxPassList"]);
    private string PasscardTitleText = ConfigurationManager.AppSettings["PasscardTitleText"];

    public pppNet ppp = new pppNet();

    protected void Page_Load(object sender, EventArgs e)
    {
        ppp.PasscodeCount = maxColumn * maxRow;
    }
    protected void GenerateSequenceKeyButton_Click(object sender, EventArgs e)
    {
        ppp.GenerateRandomSequenceKey();
        SequenceKeyTextBox.Text = ppp.SequenceKeyString;
        RebuildAll();
    }

    protected void RefreshButton_Click(object sender, EventArgs e)
    {
        if (SequenceKeyTextBox.Text.Length == 64)
        {
            ppp.SequenceKeyString = SequenceKeyTextBox.Text;
            RebuildAll();
        }
    }

    private void RebuildAll()
    {
        PrintableHyperLink.Visible = true;
        CheckPasscodeHyperLink.Visible = true;
        PrintableHyperLink.NavigateUrl = String.Format("{0}?{1}", PrintVersionHandler, ppp.SequenceKeyString);
        CheckPasscodeHyperLink.NavigateUrl = String.Format("{0}?{1}", CheckPasscodePage, ppp.SequenceKeyString);
        for (int passList = 0; passList < maxPassList; passList++)
        {

            ppp.RetrievePasscodes(passList);
            TablePlaceHolder.Controls.Add(BuildTable(passList));
        }
    }

    private Table BuildTable(int passList)
    {
        Table outputTable = new Table();
        outputTable.CssClass = "OutputTable";
        outputTable.CellPadding = 0;
        outputTable.CellSpacing = 0;
        outputTable.Rows.Add(BuildTitleRow(passList));
        outputTable.Rows.Add(BuildHeaderRow());
        for (int row = 0; row < maxRow; row++)
        {
            outputTable.Rows.Add(BuildBodyRow(row));
        }
        return outputTable;
    }

    private TableRow BuildBodyRow(int row)
    {
        TableRow bodyRow = new TableRow();
        TableCell bodyCell = new TableCell();
        bodyCell.Text = String.Format("{0,2}:", row+1);
        bodyRow.Cells.Add(bodyCell);
        for (int column = 0; column < maxColumn; column++)
        {
            bodyCell = new TableCell();
            bodyCell.Text = ppp[row * maxColumn + column];
            bodyRow.Cells.Add(bodyCell);
        }
        return bodyRow;
    }

    private TableRow BuildHeaderRow()
    {
        TableRow headerRow = new TableRow();
        headerRow.CssClass = "HeaderTableRow";
        headerRow.Cells.Add(new TableCell());
        for (int column = 0; column < maxColumn; column++)
        {
            TableCell headerCell = new TableCell();
            headerCell.Text = Char.ConvertFromUtf32(Char.ConvertToUtf32("A", 0) + column); //show column title starting from "A"
            headerRow.Cells.Add(headerCell);
        }
        return headerRow;
    }

    private TableRow BuildTitleRow(int passList)
    {
        TableRow titleRow = new TableRow();
        titleRow.CssClass = "HeaderTableRow";
        TableCell titleCell = new TableCell();
        titleCell.ColumnSpan = maxColumn;
        titleCell.Text = PasscardTitleText;
        titleRow.Cells.Add(titleCell);
        titleCell = new TableCell();
        titleCell.ColumnSpan = 0;
        titleCell.HorizontalAlign = HorizontalAlign.Right;
        titleCell.Text = String.Format("[{0}]", passList+1);
        titleRow.Cells.Add(titleCell);
        return titleRow;
    }

}